package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.admin.MarketBrandService;
import com.cskaoyan.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;

/**
 * 模块:商场管理/品牌制造商
 * url:/admin/brand
 *
 * @Author: zhang
 * @Date: 2022/11/20 14:32
 * @Since: jdk1.8
 */
@RestController
@RequestMapping("admin/brand")
public class MarketBrandController {
    @Autowired
    MarketBrandService marketBrandService;

    /**
     * 查询所有的品牌信息
     * 商品id是精确查找,商品名称是模糊查找
     * /admin/brand/list?page=1&limit=20&id=1046015&name=samsung&sort=add_time&order=desc
     *
     * @param info BasePageInfo page/limit/sort/order
     * @return BaseRespVo<CommonData < MarketBrand>>
     */
    @RequestMapping("list")
    public BaseRespVo<CommonData<MarketBrand>> list(BasePageInfo info, String id, String name) {
        if (!StringUtil.isEmpty(id)) {
            id = id.trim();
        }
        if (!StringUtil.isEmpty(name)) {
            name = name.trim();
        }
        CommonData<MarketBrand> data = marketBrandService.list(info, id, name);
        return BaseRespVo.ok(data);
    }

    /**
     * 创建一个品牌并且返回创建信息
     * /admin/brand/create
     *
     * @param marketBrand 接收到的创建信息
     * @return BaseRespVo<MarketBrand>
     * @throws SQLException 创建失败
     */
    @PostMapping("create")
    public BaseRespVo<MarketBrand> create(@RequestBody MarketBrand marketBrand) throws SQLException {
        String name = marketBrand.getName();
        name = name.trim();
        marketBrand.setName(name);
        MarketBrand data = marketBrandService.create(marketBrand);
        if (data == null) {
            return BaseRespVo.error("该品牌商名称已经存在");
        }
        return BaseRespVo.ok(data);
    }

    /**
     * 修改品牌信息
     * /admin/brand/update
     *
     * @param marketBrand 要修改的品牌信息
     * @return 返回修改后的品牌信息
     */
    @RequestMapping("update")
    public BaseRespVo<MarketBrand> update(@RequestBody MarketBrand marketBrand) {
        MarketBrand data = marketBrandService.update(marketBrand);
        return BaseRespVo.ok(data);

    }

    /**
     * 删除品牌
     * /admin/brand/delete
     *
     * @param marketBrand 要删除的品牌信息
     * @return ok
     */
    @PostMapping("delete")
    public BaseRespVo<Object> delete(@RequestBody MarketBrand marketBrand) {
        marketBrandService.delete(marketBrand);
        return BaseRespVo.ok();
    }
}
